/*
 *  Copyright 2013 National Institute of Advanced Industrial Science and Technology
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.sss.mapreduce.example;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sss.client.DataPutter;
import org.sss.client.SssClient;
import org.sss.mapreduce.GroupID;
import org.sss.mapreduce.HashPartitioner2;
import org.sss.mapreduce.SssException;
import org.sss.mapreduce.datatype.DCPackableString;
import org.sss.mapreduce.datatype.PackableInt;

public class ContMergeReduceTestPutData {
  @SuppressWarnings("unused")
  private final Logger logger = LoggerFactory.getLogger(ContMergeReduceTestPutData.class);
  private final SssClient client;
  private final int numStorages;
  private final int numSlots;
  public ContMergeReduceTestPutData(SssClient client) throws Exception {
    this.client = client;
    this.numStorages = client.getCluster().getStorageNodes().size();
    this.numSlots = 16;
  }
  private static void usage() {
    System.err.println("Usage:");
    System.err.println("java ContMergeReduceTestPutData gid0 gid1 count");
    System.err.println("   gid0: target group id");
    System.err.println("   gid1: target group id");
    System.err.println("  count: number of tuples per slot" );
    System.exit(1);
  }
  private void putData(GroupID gid0, GroupID gid1, int count)
    throws SssException, IOException {
    DataPutter<DCPackableString, PackableInt> putter0 =
      DataPutter.create(client, DCPackableString.class, PackableInt.class,
                        HashPartitioner2.class, gid0);
    DataPutter<DCPackableString, PackableInt> putter1 =
      DataPutter.create(client, DCPackableString.class, PackableInt.class,
                        HashPartitioner2.class, gid1);
    try {
      PackableInt val = new PackableInt(1);
      for (int storage=0; storage<numStorages; storage++) {
        for (int slot=0; slot<numSlots; slot++) {
          DCPackableString key = new DCPackableString
            (String.format("KEYSTRING[%02x][%02x]", storage, slot), storage, slot);
          for (int i=0; i<count; i++) {
            putter0.append(key, val);
            putter1.append(key, val);
          }
        }
      }
    }
    finally {
      putter0.close();
      putter1.close();
    }
    System.out.println("wrote " + count + " tuples on each " + numSlots +
                       " slot of both gids in " + numStorages + " storages");
  }
  /**
   * Usage:
   * java ContMergeReduceTestPutData gid0 gid1 count
   */
  public static void main(String[] args) throws Exception {
    SssClient client = new SssClient(args);
    String[] others = client.getArgs();
    if (others.length != 3) {
      usage();
    }
    GroupID gid0 = GroupID.createFromString(others[0]);
    GroupID gid1 = GroupID.createFromString(others[1]);
    int count = Integer.parseInt(others[2]);
    ContMergeReduceTestPutData app = new ContMergeReduceTestPutData(client);
    app.putData(gid0, gid1, count);
  }
}
